package com.nuoniu.sibanyun.controller.app;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.app.AppInfo;
import com.nuoniu.sibanyun.service.app.IAppInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 应用包管理 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-08-12
 */
@RestController
@RequestMapping("/appInfo")
@Api(tags = "应用包管理")
public class AppInfoController extends BaseController {

    @Autowired
    private IAppInfoService appInfoService;

    @ApiOperation("获取最新版本app")
    @GetMapping("getNewestApp")
    public HttpResult getNewestApp(Integer type) {
        if(type == null) {
            throw new NuoNiuException("类型不能为空");
        }

        return HttpResult.ok(appInfoService.getNewestApp(type));
    }


    @ApiOperation("获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, AppInfo appInfo) {
        return HttpResult.ok(appInfoService.pageList(page, appInfo));
    }

    @ApiOperation("保存app")
    @PostMapping("save")
    public HttpResult save(@RequestBody AppInfo appInfo) {
        if(StringUtils.isBlank(appInfo.getAppName())) {
            throw new NuoNiuException("名字不能为空");
        }

        if(appInfo.getType() == null) {
            throw new NuoNiuException("类型不能为空");
        }

        if(StringUtils.isBlank(appInfo.getAppVersion())) {
            throw new NuoNiuException("版本号不能为空");
        }

        if(StringUtils.isBlank(appInfo.getDownloadUrl())) {
            throw new NuoNiuException("下载地址不能为空");
        }

        appInfoService.saveApp(appInfo);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(String id) {
        appInfoService.removeById(id);
        return HttpResult.ok();
    }
}
